package com.hujh.android.bean;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hujh.android.R;

public class ItemAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater;
	private List<ItemInfo> itemInfoList;

	public ItemAdapter(Context context,
			List<ItemInfo> itemInfoList) {

		this.itemInfoList = itemInfoList;
		layoutInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return itemInfoList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return itemInfoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {

		ViewHolder viewHolder;

		// TODO Auto-generated method stub
		if (view == null) {
			view = layoutInflater.inflate(R.layout.activity_listview_item, null);
			viewHolder = new ViewHolder();
			viewHolder.icon = (ImageView) view.findViewById(R.id.icon);
			viewHolder.title = (TextView) view.findViewById(R.id.title);
			viewHolder.packageName = (TextView) view
					.findViewById(R.id.packagename);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		viewHolder.icon.setImageDrawable(itemInfoList.get(position)
				.getIcon());
		viewHolder.title.setText(itemInfoList.get(position)
				.getName());
		viewHolder.packageName.setText(itemInfoList.get(position)
				.getPackageName());

		return view;
	}

	private class ViewHolder {
		ImageView icon;
		TextView title;
		TextView packageName;
	}

}
